package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseDbService;
import com.dw.etl.core.bean.model.BaseDb;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * baseDb控制器
 * Created by hozeData on 2021/05/21.
 */
@RestController
@RequestMapping("/etl/baseDb/")
@Api( tags="数据源")
@CrossOrigin(origins = "*", maxAge = 3600)
public class BaseDbController {

    @Autowired
    BaseDbService baseDbService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody BaseDb baseDb, Integer pageNum, Integer pageSize) {
        PaginationVO<BaseDb> pageVO = baseDbService.findPageByModel(baseDb, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit) {
        List<BaseDb> pageVO = baseDbService.findByModel(new BaseDb());
        return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="baseDb", value="实体", paramType="body", required=true, dataType="BaseDb")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody BaseDb baseDb, HttpServletRequest request)  {
        int num ;
        if(baseDb.getIdDb()==null) {
            num = baseDbService.insertSelective(baseDb);
        } else {
            num = baseDbService.updateByIdSelective(baseDb);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idDb", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(Long idDb, HttpServletRequest request) throws BaseException {
        BaseDb baseDb = baseDbService.getById(idDb);
        return new BaseResult(CommonResultConstant.SUCCESS, baseDb);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="baseDb", value="实体", paramType="body", required=true, dataType="BaseDb")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody BaseDb baseDb, HttpServletRequest request)  {
        int num = baseDbService.updateByIdSelective(baseDb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idDb", value="主键", paramType="query", required=true, dataType="Long")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(Long idDb, HttpServletRequest request) throws BaseException {
        int num = baseDbService.deleteById(idDb);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }



}
